package com.baizhi.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.baizhi.entity.Record;
import com.baizhi.entity.Student;
import com.baizhi.service.IRecordService;
import com.baizhi.util.JwtUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author longyang
 * @since 2022-01-11
 */
@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private IRecordService recordService;
    //查询全部奖惩信息
    @GetMapping("selectAllRecord")
    public PageInfo<Record> selectAllRecord(int pageNum, int pageSize){
        PageInfo<Record> recordPageInfo = recordService.selectAllRecord(pageNum, pageSize);
        return recordPageInfo;
    }
    //修改奖惩信息
    @PostMapping("updateRecord")
    public int updateRecord(Record record){
        Integer integer = recordService.updateRecord(record);
        return integer;
    }
    //删除奖惩信息
    @DeleteMapping("deleteRecord")
    public Integer deleteRecord(Integer rid){
        Integer integer = recordService.deleteRecord(rid);
        return integer;
    }
    //添加奖惩信息
    @PutMapping("addRecord")
    public Integer addRecord(Record record,@RequestHeader("Authorization") String token){
        //插入前给record的创建人和创建时间复制
        // 后期要从登陆人里面获取信息
        System.out.println(record);

        DecodedJWT verify = JwtUtils.verify(token);
        String username = verify.getClaim("username").asString();
        record.setCreateBy(username);//要从登陆人信息里面获取
        //LocalDateTime.now()获取到系统时间
        record.setCreateDate(new Date());
        Integer integer = recordService.addRecord(record);
        return integer;
    }
    //根据奖惩名称模糊查询
    @GetMapping("selcetByRname")
    public List<Record> selcetByRname(String Rname){
        List<Record> records = recordService.selcetByRname(Rname);
        return records;
    }
//    //根据学生姓名模糊查询奖惩
//    @GetMapping("selcetBystuname")
//    List<Student> selcetBystuname(String stuname){
//        List<Student> students = studentService.selcetBystuname(stuname);
//        return students;
//    }
}

